//
//  InspirationViewController.m
//  PerfectWorld
//
//  Created by dllo on 16/9/19.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "InspirationViewController.h"
#import "InspirationCollectionViewCell.h"
#import "AFNetworking.h"
#import "SonUser.h"
#import "SonUserImage.h"
#import "ideabooks.h"
#import "IdeaPhotos.h"
#import "InspirationSonViewController.h"
@interface InspirationViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)ideabooks *model;
@property (nonatomic, strong) NSMutableArray *modelArr;
@end

@implementation InspirationViewController
/**
 *  灵感集
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    self.modelArr = [NSMutableArray array];
    [self handleData];
    [self creatCollectionView];
}
- (void)creatCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(self.view.bounds.size.width / 2 - 10, self.view.bounds.size.height / 4);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 50) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.directionalLockEnabled = YES;
    self.collectionView.alwaysBounceVertical = YES;
    [self.view addSubview:_collectionView];
    [_collectionView registerNib:[UINib nibWithNibName:@"InspirationCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cell"];
}
// 灵感集
- (void)handleData {
    NSString *nameStr = [_urlID stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *footStr = [@"木木" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     NSString *strURL = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/user/%@/ideabooks?start=0&count=10&user=%@", nameStr, footStr];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:strURL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        for (NSDictionary *temp in [dic objectForKey:@"ideabooks"]) {
            self.model = [ideabooks modelWithDic:temp];
            [_modelArr addObject:_model];
        }
         [_collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error = %@", error);
    }];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _modelArr.count;
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    InspirationSonViewController *sonVC = [[InspirationSonViewController alloc] init];
     sonVC.urlStr = _model.Idea_id.description;
    [self.navigationController pushViewController:sonVC animated:YES];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    InspirationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = _modelArr[indexPath.row];
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
